package com.tiancheng.commom.pay.lkl;

import com.lkl.laop.sdk.Config;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.*;
import com.tiancheng.commom.pay.lkl.res.*;
import com.tiancheng.trade.commom.core.exception.Asserts;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;

/**
 * 拉卡拉支付通道配置
 */
public class LklPay {
    private final LklPayProperties lklPayProperties;

    public LklPay(LklPayProperties config) throws SDKException {
        this.lklPayProperties = config;
        Config lklConfig = new Config();
        lklConfig.setAppId(config.getAppId());
        lklConfig.setSerialNo(config.getSerialNo());
        lklConfig.setPriKeyPath(config.getPriKeyPath());
        lklConfig.setLklCerPath(config.getLklCerPath());
        lklConfig.setLklNotifyCerPath(config.getLklNotifyCerPath());
        lklConfig.setServerUrl(config.getServerUrl());
        lklConfig.setSm4Key(config.getSm4Key());
        boolean initSC = LKLSDK.init(lklConfig);
        Asserts.isTrue(initSC, "拉卡拉交易渠道初始化失败");
    }

    /**
     * 微信支付
     *
     * @param params
     * @return GcBaseResp<GcWxPayRes>
     */
    public LklBaseRes<LklPreorderRes> wechatPay(V3LabsTransPreorderEncryRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklPreorderRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 支付宝支付
     *
     * @param params
     * @return GcBaseResp<GcWxPayRes>
     */
    public LklBaseRes<LklPreorderRes> aliPay(V3LabsTransPreorderEncryRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklPreorderRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询订单信息
     *
     * @param params
     * @return GcBaseResp<GcWxPayRes>
     */
    public LklBaseRes<LklOrderQueryRes> queryOrderPay(V3LabsQueryTradequeryRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklOrderQueryRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 订单退款
     *
     * @param params
     * @return GcBaseResp<LklRefundRes>
     */
    public LklBaseRes<LklRefundRes> refund(V3LabsRelationRefundRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklRefundRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭订单
     *
     * @param params
     * @return GcBaseResp<LklRefundRes>
     */
    public LklBaseRes<String> close(V3LabsRelationCloseRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            LklBaseRes<Map> response = getResponse(res, Map.class);

            LklBaseRes<String> result = new LklBaseRes<>();
            result.setCode(response.getCode());
            result.setMsg(response.getMsg());
            result.setRespTime(response.getRespTime());
            result.setRespData(response.getMsg());
            return result;
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询退款订单
     *
     * @param params
     * @return GcBaseResp<LklRefundRes>
     */
    public LklBaseRes<LklOrderQueryRes> getRefund(V3LamsTradeTradeRefundQueryRequest params) {
        //响应信息
        try {
            params.setLklAppId(this.lklPayProperties.getAppId());
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            params.setTermNo(this.lklPayProperties.getTermNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklOrderQueryRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 订单分账申请
     *
     * @param params
     * @return GcBaseResp<LklRefundRes>
     */
    public LklBaseRes<LklSeparateRes> orderSettle(V3SacsSeparateRequest params) {
        //响应信息
        try {
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklSeparateRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * 订单分账-退回
     *
     * @param params
     * @return GcBaseResp<LklRefundRes>
     */
    public LklBaseRes<LklSeparateFallbackRes> orderSettleFallback(V3SacsFallbackRequest params) {
        //响应信息
        try {
            params.setMerchantNo(this.lklPayProperties.getMerchantNo());
            String res = LKLSDK.httpPost(params, true, false);
            return getResponse(res, LklSeparateFallbackRes.class);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * 高诚 接口返回结果转换
     *
     * @param resStr
     * @param bodyClass
     * @param <E>
     * @return
     */
    private <E> LklBaseRes<E> getResponse(String resStr, Class<E> bodyClass) {
        if (StringUtils.isEmpty(resStr)) {
            return null;
        }
        Map<String, Object> map = JsonUtil.fromJson(resStr, Map.class);
        LklBaseRes<E> result = new LklBaseRes<>();
        result.setCode(MapUtils.getString(map, "code"));
        result.setMsg(MapUtils.getString(map, "msg"));
        Object data = MapUtils.getObject(map, "resp_data");
        if (Objects.nonNull(data)) {
            result.setRespData(JsonUtil.fromJson(JsonUtil.toJsonString(data), bodyClass));
        }
        return result;
    }
}
